<?php
ob_start();
session_start();
$fileFB=fopen("../../facebook.txt","r");
$leggiFB=@fread($fileFB,filesize("../../facebook.txt"));  
$id_app=nl2br($leggiFB);
  
  $link = "https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=";
  
  $split_uri= explode("Service",$_SERVER['REQUEST_URI']);
  $final_uri=$_SERVER['HTTP_HOST'].$split_uri[0];
  
  $uri = "http://".$final_uri."Service/Facebook/facebook.php?socialnetwork=Facebook";
  $scope = "email, publish_stream, read_stream, offline_access,user_birthday,user_location,read_friendlists,read_insights,manage_pages";
  $data = NULL;
  
  $li =$link.$id_app.'&redirect_uri='.$uri.'&scope='.$scope;

  if(!isset($_GET['socialnetwork'])){
      header("Location: " . $li);
     
  }    
  
  
 
?>
<script type="text/javascript">

checkFB();

function checkFB(){

 url = AccessTokenFB();
 
 //document.write(url);
 
 Invia_TokenFB(url);


}

function AccessTokenFB(url){
  var url = ""+window.location;
  
  //document.write(url);

  //try{
         var token =  url.split('?');
         var socialnet = token[1];
         var sn = token[1].substring(14,16);
         //alert(sn);

         var socialnet =  url.split('#');
         //alert(socialnet);

         var accesstoken = socialnet;
         //alert(accesstoken);

         var at = accesstoken[1].split('&');
         var access_token = at[0].substring(13);
         //alert(access_token);
         
         var url = new Array();

         url[0] = access_token;
         url[1] = sn;
        
         return url;
         

  //}catch(e){

      document.write(e);

  //}

}

  function Invia_TokenFB(url){
    var urlimg = "https://graph.facebook.com/me/picture?access_token=";
    var access_token = url[0];
    var final_uri = "<?php echo$final_uri; ?>";
    location.href= 'http://'+final_uri+'Service/insert_token.php?access_token='+access_token+'&service=Facebook&urlimg='+urlimg;
  }
  
</script>